package com.shushan.home.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.shushan.home.bean.ToyAwardBean;
import com.shushan.shushanbao.R;
import com.shushan.home.bean.RewardToyBean;
import com.shushan.util.ImageUtils;

import java.util.List;


/**
 * @ClassName: AwardToyAdapter
 * @Desciption: //玩具奖励条目的适配器
 * @author: zhangshihao
 * @date: 2018-04-09
 */
public class AwardToyListAdapter extends BaseAdapter{

    private List<RewardToyBean> awardList;
    private Context mContext;
    private OnStartButtonClickListener listener;

    public AwardToyListAdapter(Context context, List<RewardToyBean> awardList) {
        this.mContext = context;
        this.awardList = awardList;
    }

    public void setOnStartButtonClickListener(OnStartButtonClickListener listener){
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return awardList.size();
    }

    @Override
    public Object getItem(int position) {
        return awardList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (null == convertView) {
            //填充布局
            convertView =  LayoutInflater.from(mContext).inflate(R.layout.item_award_toy, parent,false);
            holder = new ViewHolder();
            //通过ID查询控件
            holder.toyPic = convertView.findViewById(R.id.iv_toy_award_pic);
            holder.toyName = convertView.findViewById(R.id.tv_toy_name);
            holder.completeCondition = convertView.findViewById(R.id.tv_complete_condition);
            holder.completeExercising = convertView.findViewById(R.id.tv_complete_exercising);
            holder.btnStartExercise = convertView.findViewById(R.id.btn_start_exercise);

            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        RewardToyBean bean = awardList.get(position);
        holder.toyName.setText(bean.name);
        holder.completeCondition.setText(bean.task);
        holder.completeExercising.setText(getCompleteExercisingStr(bean.complete_num,bean.training_num));
        ImageUtils.loadWXImageView(mContext,bean.pic,holder.toyPic);
        final int pos = position;
        final View v = convertView;
        if(listener != null){
            holder.btnStartExercise.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onStartButtonClick(v,pos);
                }
            });
        }

        return convertView;
    }

    public static class ViewHolder{
         ImageView toyPic;
         TextView toyName;
         TextView completeCondition;
         TextView completeExercising;
         Button btnStartExercise;
    }

    private String getConditionStr(int times,int exerciseType){
        StringBuffer sb = new StringBuffer();
        sb = sb.append("再完成")
                .append(String.valueOf(times))
                .append("个")
                .append(ToyAwardBean.getExerciseNameByType(exerciseType))
                .append("就可以\n获得");
        return sb.toString();
    }

    private String getCompleteExercisingStr(String completeNum,String exercisingNum){
        StringBuffer sb = new StringBuffer();
        sb = sb.append(completeNum)
                .append("人已经完成 | ")
                .append(exercisingNum)
                .append("人正在练习");
        return sb.toString();
    }

    public interface OnStartButtonClickListener{
        void onStartButtonClick(View itemView, int position);
    }

}
